package com.quectel.sys.module.sys.service.impl.mongo;

import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.constant.global.SystemConstants;
import com.quectel.sys.constants.MongoCollectionConstants;
import com.quectel.sys.module.sys.dto.mongo.SysLogDto;
import com.quectel.sys.module.sys.entity.mongo.SysLogModel;
import com.quectel.sys.module.sys.service.SysLogService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.common.DateUtils;
import com.quectel.util.common.Request;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mongo.MongodbUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@DubboService
public class SysLogServiceImpl implements SysLogService {
    @Override
    public List<SysLogDto> queryList(Map<String, Object> params) {
        Request request = Request.configParams(params);
        return CopyUtils.copyList(
                MongodbUtils.page(
                        new Query(sqlWhere(params)),
                        request.getPageNo() - 1,
                        request.getPageSize(),
                        Sort.by(Sort.Direction.DESC, SystemConstants.MONGO_ENTITY_ID_FLAG),
                        SysLogModel.class,
                        MongoCollectionConstants.SYS_LOG_COLLECTION
                ), SysLogDto.class);
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        Criteria criteria = sqlWhere(params);
        return (int) MongodbUtils.count(new Query(criteria), MongoCollectionConstants.SYS_LOG_COLLECTION);
    }

    private Criteria sqlWhere(Map<String, Object> params) {
        Criteria criteria = new Criteria();
        Request request = Request.configParams(params);
        String account = request.getString("account");
        String log = request.getString("log");
        Date startTime = request.getDate("startTime", DateUtils.FormatType.COMMON.getType());
        Date endTime = request.getDate("endTime", DateUtils.FormatType.COMMON.getType());

        Object tenant = null;
        Map<String, Object> _loginUserSession = (Map<String, Object>) params.get("_loginUserSession");
        if (_loginUserSession != null) {
            tenant = _loginUserSession.get(LambdaUtil.getFieldName(SysLogModel::getTenantId));
        }
        if (tenant != null) {
            criteria.and(LambdaUtil.getFieldName(SysLogModel::getTenantId)).is(Long.valueOf(String.valueOf(tenant)));
        }
        if (startTime != null && endTime != null) {
            criteria.and(LambdaUtil.getFieldName(SysLogModel::getCreateTime)).gte(startTime).lte(endTime);
        }
        if (StringUtils.isNotBlank(account)) {
            criteria.and(LambdaUtil.getFieldName(SysLogModel::getAccount)).is(account);
        }
        if (StringUtils.isNotBlank(log)) {
            String regex = String.format("%s%s%s", "^.*", log, ".*$");
            Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
            criteria.and(LambdaUtil.getFieldName(SysLogModel::getLog)).regex(pattern);
        }
        return criteria;
    }

    @Override
    public Long save(SysLogDto dto) {
        SysLogModel sysLogModel = CopyUtils.copyObj(dto, SysLogModel.class);
        sysLogModel.setId(Snowflake.nextId());
        MongodbUtils.add(sysLogModel, MongoCollectionConstants.SYS_LOG_COLLECTION);
        return sysLogModel.getId();
    }

}
